<?php
require_once(Config::acquire('Controller', null, 'controller'));
require_once(Config::acquire('Question', MODEL));
require_once(Config::acquire('Diensten', MODEL));
require_once(Config::acquire('Workaround', MODEL));
require_once(Config::acquire('Oplossing', MODEL));


/**
 * Created by JetBrains PhpStorm.
 * User: Sander
 * Date: 26-6-11
 * Time: 12:52
 * To change this template use File | Settings | File Templates.
 */
 
class QuestionController extends Controller{

     /**
     * info over question
     */
    public function index()
    {
        $this->view->render("Question.tpl");
    }

     /**
     * Overzicht van de gegevens binnen question met klikken op ID naar detailoverzicht
     */
    public function overzicht()
    {
        $list = array();
             foreach (Question::readAll() as $question)
                 $list[] = $question->toArray();
             $this->view->assign('QuestionTitle', "Question Overzicht");
             $this->view->assign('detail_link', SDS::getURL('Question', 'detail'));
             $this->view->assign("question", $list);
             $this->view->assign('dienst_link', SDS::getURL('Diensten', 'overzicht'));
             $this->view->render('Question_list.tpl');
    }

     /**
     * Gedetailleerd overzicht met klikken op wijzigen of verwijderen naar desbetreffende pagina
     */
    public function detail()
    {
        $params = UrlDecoder::get_Params();

        if (is_null($params[0]))
            SDS::redirect("Question", "overzicht");
        $id = $params[0];
        foreach (Question::read($id)->toArray() as $key => $value) {
            $this->view->assign($key, $value);
        }
     //   $this->view->assign("form_URL", SDS::getURL('Question', 'add', array($id)));
        $this->view->assign('wijzig_url', SDS::getURL('Question', 'bewerken', array($params[0])));
        $this->view->assign('verwijder_url', SDS::getURL('Question', 'delete', array($params[0])));
        $this->view->render('Question_Detail.tpl');

    }

     /**
     * Toevoegen van nieuwe vraag
     */
    public function toevoegen()
    {
        $workaroundIDs = array();
        $oplossingIDs = array();
        $dienstIDs = array();
        foreach (Diensten::readAll() as $diensten) {
            $dienstIDs[$diensten->getID()] = $diensten->getNaam();
        }
        foreach (Workaround::readAll() as $workaround) {
            $workaroundIDs[$workaround->getID()] = $workaround->getOmschrijving();
        }
        foreach (Oplossing::readAll() as $oplossing) {
            $oplossingIDs[$oplossing->getID()] = $oplossing->getOmschrijving();
        }

        $this->view->assign('diensten', $dienstIDs);
        $this->view->assign('workaround', $workaroundIDs);
        $this->view->assign('oplossing', $oplossingIDs);
        $this->view->assign('form_URL', SDS::getURL('Question', 'add', null));
        $this->view->assign('questionTitle', 'Question toevoegen');
        $this->view->render("Question_Change.tpl");
    }

     /**
     * Mogelijkheid om een bestaand item te veranderen
     */
    public function bewerken()
    {
        $params = UrlDecoder::get_Params();
        if (!isset($params[0]))
            SDS::redirect("Question", "overzicht");
        $id = $params[0];
        foreach (Question::read($id)->toArray() as $key => $value) {
            $this->view->assign($key, $value);
        }
        $this->view->assign('form_URL', SDS::getURL('Question', 'update', array($id)));
        $this->view->assign('QuestionTitle', 'Question Wijzigen');
        $this->view->render("Question_Change.tpl");

    }

     /**
     * Toevoegen van nieuw item
     */
    public function add()
    {
        require_once(Config::acquire('FormValidation', APPLICATION));
        FormValidation::stripInput();
        Question::create(null,
                            $_POST['Omschrijving'],
                            $_POST['Diensten'],
                            $_POST['Workaround'],
                            $_POST['Oplossing'])->update();
        SDS::redirect("Question", "overzicht");
    }


     /**
     * Deleten van een vraag
     */
    public function delete()
    {
          $id = UrlDecoder::get_Params();
        if (isset($id[0])) {
            Question::read($id[0])->delete();
            SDS::redirect("Question", "overzicht", null);
        }
    }


     /**
     * updaten van een bestaand item
     */
    public function update()
    {
        require_once(Config::acquire('FormValidation', APPLICATION));
        FormValidation::stripInput();
        $params = UrlDecoder::get_Params();
        if (isset($params[0])) {
            $question = Question::read($params[0]);
            $question->setOmschrijving($_POST['Omschrijving']);
            $question->setOmschrijving($_POST['Diensten']);
            $question->setOmschrijving($_POST['Workaround']);
            $question->setOmschrijving($_POST['Oplossing']);
            $question->update();
            SDS::redirect("Question", "wijzigen", $params[0]);
        }
        SDS::redirect("Question", "overzicht", null);
    }
}
